//
//  ProvinceAbbrView.m
//  CarLife
//
//  Created by yu on 2017/10/31.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "ProvinceAbbrView.h"

@interface ProvinceAbbrView ()
{
    UIView * _autoMarginViewsContainer;
}

@end

@implementation ProvinceAbbrView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor clearColor];
        [self setupAutoMarginViewsWithItemWidth:35];
    }
    return self;
}

// 设置一排固定宽度自动间距子view
- (void)setupAutoMarginViewsWithItemWidth:(CGFloat)itemWidth
{
    _autoMarginViewsContainer = [[UIView alloc] init];
    [self addSubview:_autoMarginViewsContainer];
    
    if (@available(iOS 13.0, *)) {
        UIColor *bgColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
                return kRgb(227, 227, 227);
            } else {
                return UIColor.tertiarySystemBackgroundColor;
            }
        }];
        _autoMarginViewsContainer.backgroundColor = bgColor;
        self.backgroundColor = bgColor;
    } else {
        _autoMarginViewsContainer.backgroundColor = kRgb(227, 227, 227);
        self.backgroundColor = kRgb(227, 227, 227);
    }
    
    NSArray *abbrs = @[@"京",@"沪",@"川",@"浙",@"闽",@"冀",@"吉",@"辽",@"鲁",@"豫",@"苏",@"湘",@"陕",@"粤",@"鄂",@"皖",@"云",@"新",@"宁",@"琼",@"渝",@"津"];
    
    NSMutableArray *temp = [NSMutableArray new];
    for (int i = 0; i < abbrs.count; i++) {
        UIButton *btn = [[UIButton alloc] init];
        btn.tag = i + 10;
        [btn setTitle:abbrs[i] forState:UIControlStateNormal];
        if (@available(iOS 13.0, *)) {
            [btn setTitleColor:UIColor.labelColor forState:UIControlStateNormal];
        } else {
            [btn setTitleColor:kTextBlackColor forState:UIControlStateNormal];
        }
        [btn addTarget:self action:@selector(chooseProAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = SYSTEM_FONT(15);
        [btn setBackgroundImage:IMAGE_NAMED(@"shadow") forState:UIControlStateNormal];
        [_autoMarginViewsContainer addSubview:btn];
        btn.sd_layout.autoHeightRatio(1.1);

        [temp addObject:btn];
    }
     /**
     * verticalMargin   : 视图之间的垂直间距
     * vInset           : 上下缩进值
     * hInset           : 左右缩进值
     */
    [_autoMarginViewsContainer setupAutoMarginFlowItems:[temp copy] withPerRowItemsCount:9 itemWidth:itemWidth verticalMargin:12 verticalEdgeInset:12 horizontalEdgeInset:8];
//    _autoMarginViewsContainer.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
    _autoMarginViewsContainer.sd_layout
    .leftSpaceToView(self, 0)
    .rightSpaceToView(self, 0)
    .topSpaceToView(self, 0)
    .bottomSpaceToView(self, kSafeAreaBottomHeight);
    
    UIButton *finishBtn = [[UIButton alloc] init];
    [_autoMarginViewsContainer addSubview:finishBtn];
    [finishBtn addTarget:self action:@selector(finishAction) forControlEvents:UIControlEventTouchUpInside];
    [finishBtn setBackgroundImage:IMAGE_NAMED(@"finish") forState:UIControlStateNormal];
    
    finishBtn.sd_layout
    .rightSpaceToView(_autoMarginViewsContainer, 10)
    .bottomSpaceToView(_autoMarginViewsContainer, 15)
    .widthIs(60)
    .heightIs(35);
    
}

- (void)chooseProAction:(UIButton *)btn {
    if (self.delegate && [self.delegate respondsToSelector:@selector(chooseProvinceAbbr:)]) {
        [self.delegate chooseProvinceAbbr:btn.titleLabel.text];
    }
}

- (void)finishAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(finishAction)]) {
        [self.delegate finishAction];
    }
}

@end
